package com.hz.controller;


import com.hz.pojo.Admin;
import com.hz.pojo.User;
import com.hz.service.AdminService;
import com.hz.service.UserService;
import com.hz.utils.MassageJson;
import com.hz.utils.MassageJsons;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    AdminService adminService;
    @Resource
    UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(value = "/getadmin",method = RequestMethod.POST)
    public MassageJson getAdmin(String admin_id){
        Admin admin = null;
        try{
            admin = adminService.getAdmin(admin_id);
            System.out.println("管理员的id是"+admin_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(admin!=null){
            return  new MassageJson(200,"请求成功",admin);
        }else {
            return  new MassageJson(201,"请求失败",admin);
        }
    }

    //管理员登录
    @RequestMapping("/logadmin")
    public MassageJson logAdmin(@RequestBody HashMap<String,Object> map){
        Admin admin = null;
        System.out.println(map.size()+"+++++++++++++++++++++++");
        try{
            admin = adminService.logAdmin(map.get("admin_name")+"",map.get("admin_password")+"");
        }catch (Exception e){
            e.printStackTrace();
        }
        if(admin!=null){
            return  new MassageJson(200,"登录成功",admin);
        }else {
            return  new MassageJson(201,"登录失败",admin);
        }
    }

    @RequestMapping(value = "/addAdmin",method = RequestMethod.POST)
    public MassageJson addAdmin(Admin admin){

        int i = 0;
        try{
            i = adminService.addAdmin(admin);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i>0){
            return new MassageJson(200,"添加成功",admin);
        }else {
            return new MassageJson(201,"添加失败",admin);
        }

    }

    @RequestMapping(value = "/delAdmin",method = RequestMethod.POST)
    public MassageJson delAdmin(String admin_id){
        int i = 0;
        try{
            i = adminService.delAdmin(admin_id);
            System.out.println("要删除的管理员id是"+admin_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i>0){
            return  new MassageJson(200,"删除成功",i);
        }else {
            return  new MassageJson(201,"删除失败",i);
        }
    }

    @RequestMapping(value = "/selAdmin",method = RequestMethod.POST)
    public MassageJsons selAdmin(){
        List<Admin> adminList = null;
        try {
            adminList=adminService.selAdmin();
            if(adminList!=null){
                return new MassageJsons("200","请求成功",adminList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new MassageJsons("201","登录失败",adminList);
    }


    //查询所有用户，分页，名称模糊查询
    @RequestMapping("/selUser")
    public MassageJsons selUser(Integer pages,Integer pageSize,String inquire){
        List<User> userList = null;
        try {
            /*start:就是当前页的起始索引,pageSize就是每页的条数
            pages:就是当前页
            */
            int start=(pages-1)*pageSize;
            //执行数据库分页查询方法
            userList=adminService.selUser(start,pageSize,inquire);
            ;
            if(userList!=null){
                //返回查询数据，返回总条数
                return new MassageJsons("200","请求成功",userList,adminService.getUserCounts(inquire));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new MassageJsons("201","请求失败",userList);
    }

    //更改用户代理等级
    @RequestMapping("/udpUserProxyId")
    public MassageJsons udpUserProxyId(Integer proxy_id,String open_id){

        try {
            int i =adminService.udpUserProxyId(proxy_id,open_id);
            if(i!=0){new MassageJsons("0","更改成功",null);}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MassageJsons("1","更改失败");
    }
}
